package com.example.finalprojectclient3;

import java.util.Locale;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicHeader;
import org.apache.http.params.HttpConnectionParams;
import org.apache.http.protocol.HTTP;
import org.apache.http.util.EntityUtils;
import org.json.JSONObject;

import android.app.Activity;
import android.os.Bundle;
import android.os.Looper;
import android.speech.tts.TextToSpeech;
import android.speech.tts.TextToSpeech.OnInitListener;
import android.text.TextUtils;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.example.finalprojectclient.R;
 
public class MainActivity extends Activity implements OnInitListener{
	private static String SERVER = "http://speechforcs496.appspot.com/finalprojectserver";
	private static String TESTSERVE="http://posttestserver.com/post.php";
	private int flag = 0;
	private EditText value;
	private EditText value2;
	private Button btn;
	//private ProgressBar pb;
	private TextView res;
	private String output;
	private TextToSpeech tts;
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		value=(EditText)findViewById(R.id.editText1);
		value2=(EditText)findViewById(R.id.editText2);
		//btn=(Button)findViewById(R.id.button1);
		//pb=(ProgressBar)findViewById(R.id.progressBar1);
		//pb.setVisibility(View.GONE);
		//btn.setOnClickListener(this);
		res=(TextView)findViewById(R.id.text);
		tts = new TextToSpeech(this, this);
	}
 
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}
	
	public void onClick(View v) {
		TextUtils objs = null;
		
		
		
		// TODO Auto-generated method stub
		if(value.getText().toString().length()<1  || value2.getText().toString().length()<1 ){
			// out of range
			output = "No Empty Values Please;";
			Toast.makeText(this, output, Toast.LENGTH_LONG).show();
			tts.speak( output + "ohms", TextToSpeech.QUEUE_FLUSH, null);
		}else{
		//	pb.setVisibility(View.VISIBLE);
			Toast.makeText(this, "sending information", Toast.LENGTH_LONG).show();
			int arg1 = Integer.parseInt(value.getText().toString());
			int arg2 = Integer.parseInt(value2.getText().toString());
			sendJson(arg1, arg2);		
			res.setText(output);	
			tts.speak( output + "ohms", TextToSpeech.QUEUE_FLUSH, null);
		}
		}
		
	
	
	public void updateTextView(String toThis) {

		res.setText(toThis);
		
	}
 
	protected void sendJson(final int val1, final int val2) {
        Thread t = new Thread() {

            public void run() {
                Looper.prepare(); //For Preparing Message Pool for the child Thread
                HttpClient client = new DefaultHttpClient();
                HttpConnectionParams.setConnectionTimeout(client.getParams(), 10000); //Timeout Limit
                HttpResponse response;
                JSONObject json = new JSONObject();

                try {
                    HttpPost post = new HttpPost(SERVER);
                    json.put("val1", val1);
                    json.put("val2", val2);
                    StringEntity se = new StringEntity( json.toString());  
                    se.setContentType(new BasicHeader(HTTP.CONTENT_TYPE, "application/json"));
                    post.setEntity(se);
                    response = client.execute(post);
                    
                    /*Checking response */
                    if(response!=null){
                   	HttpEntity urlEntity = response.getEntity();
                	output = EntityUtils.toString(urlEntity, "UTF-8");
                    }

                } catch(Exception e) {
                    e.printStackTrace();
                  
                }

                Looper.loop(); //Loop in the message queue
            }
        };

        t.start();      
    }

	@Override
	public void onInit(int status) {
		// TODO Auto-generated method stub
		if (status == TextToSpeech.SUCCESS)
		{
			tts.setLanguage(Locale.US);
			
		}
		
		
	}
		}